﻿//========================================
// All rights reserved (C) 2017-2100
// CLR 版本：	4.0.30319.42000
// 命名空间:	SFJDService.SocketHandle
// 文 件 名：	PH07OnLine
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2017/9/27 14:46:31
//========================================
using Network.UpdateData;
using System;
using System.IO;
using System.Net.Sockets;

namespace SFJDService.SocketHandle
{
    /// <summary>
    /// 用户上线消息
    /// Packet: userName, version
    /// </summary>
    class PH07OnLine : PacketHandle
    {
        public PH07OnLine(Socket socket, Packet packet) : base(socket, packet)
        {
        }

        protected override void Handle()
        {
            string[] str = Message.Split('♠');
            Version net_version = new Version(str[1]);
            Runtime.UsersOnLine.HanleActive(guidSend, GetIpSend(), str[0]);
            Launcher.SendMsg2All(typeof(PH07ActivingUser).GetDictianaryKey(), Runtime.thisUser.UserName);//发出用户激活消息
            bool hasUpdate = false;
            if(Runtime.version < net_version)//判断本地文件版本是否最新
            {
                FileInfo fileInfo = null;
                Version fileVersion = null;
                try
                {
                    fileInfo = new FileInfo(Runtime.pathUpdate + "\\SFJDService.exe");
                }
                catch { }
                if(fileInfo != null && fileInfo.Exists)//是则用本地文件升级
                {
                    fileVersion = new Version(System.Diagnostics.FileVersionInfo.GetVersionInfo(Runtime.pathUpdate + "\\SFJDService.exe").FileVersion);
                    if(fileVersion >= net_version)
                    {
                        System.Diagnostics.Process.Start(Runtime.pathUpdate + "\\Update.exe");
                        hasUpdate = true;
                    }
                }
                if(!hasUpdate)//否则网络更新
                {
                    Launcher.SendMsg2All(typeof(PH06GetVersion).GetDictianaryKey(), Runtime.version.ToString());
                    Delay(1000 * 10);
                    //if(Runtime.UsersOnLine.activeUsers.Count > 1)
                    //{
                    //    ClassActive active = Runtime.UsersOnLine.activeUsers[0];
                    //    active.Clone(Runtime.UsersOnLine.activeUsers[0]);
                    //    foreach (ClassActive user in Runtime.UsersOnLine.activeUsers)
                    //    {
                    //        if(user.Version > user.Version)
                    //        {
                    //            active.Clone(user);
                    //            SendUdp(typeof(PH06GetUpdateFiles).GetDictianaryKey(), Runtime.thisUser.UserGuid, user.Guid, Runtime.version.ToString());
                    //        }
                    //    }
                    //}
                }
            }
        }
    }
}
